package org.thingsboard.server.dao.video;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.video.Video;
import org.thingsboard.server.dao.model.sql.VideoEntity;
import org.thingsboard.server.dao.sql.video.VideoRepository;

import java.util.*;
import java.util.stream.Collectors;
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {
    @Autowired
    VideoRepository videoRepository;

    @Override
    public Video saveOrUpdate(Video videoDto) {
        VideoEntity videoEntity = new VideoEntity();
        BeanUtils.copyProperties(videoDto, videoEntity);
        videoEntity.setOrgCode(videoDto.getOrgCode());
        videoEntity.setCreatedTime(new Date());
        videoEntity.setMinute(0);
        videoEntity.setTenantId(videoDto.getTenantId());
        videoRepository.save(videoEntity);
        videoDto.setId(videoEntity.getId());
        return videoDto;
    }

//package org.thingsboard.server.dao.model.sql;
    @Override
    public Video getByCamId(String vid) {
        Video video = new Video();
        Specification<VideoEntity> spec = (Specification) (root, query, builder) -> {
            return query.where( builder.equal(
                    root.get("cameraId"),
                    vid
            )).getRestriction();
        };
        VideoEntity vdb = videoRepository.findOne(spec).orElse(null);
        if(vdb==null){
            return null;
        }
        BeanUtils.copyProperties(vdb, video);
        return video;
    }


    @Override
    public Page<Video> page(Pageable page, String textSearch, TenantId tenantId) {
        Page<VideoEntity> db = null;
        if (textSearch == "" || textSearch == null) {
            Specification<VideoEntity> spec = (Specification) (root, query, builder) -> {
                return query.where( builder.equal(
                        root.get("tenantId"),
                        tenantId.getId()
                )).getRestriction();
                //query.where(builder.like(root.get("name"),"%"+textSearch+"%")).getRestriction();
            };
            // 判断分页查询条件是否为空，若为空，则查询所有数据，否则，根据条件进行查询
            db = videoRepository.findAll(spec,page);
        } else {
            Specification<VideoEntity> spec = (Specification) (root, query, builder) -> {
                return query.where(builder.and(builder.equal(
                        root.get("tenantId"),
                        tenantId.getId()
                ), builder.or(builder.like(root.get("name"), "%" + textSearch + "%"),
                        builder.like(root.get("ip"), "%" + textSearch + "%")))).getRestriction();
            };
            db = videoRepository.findAll(spec, page);
        }

        List<Video> collect = db.getContent().stream().map(i -> {
            Video streamHandle = new Video();
            BeanUtils.copyProperties(i, streamHandle);
            return streamHandle;
        }).collect(Collectors.toList());

        ArrayList<Video> o1 = new ArrayList<>();
        o1.addAll(collect);
        Page<Video> resp = new PageImpl<>(o1, db.getPageable(), db.getTotalElements());
        // BeanUtils.copyProperties(db,resp);
        return resp;
    }

    @Override
    public void delById(UUID id) {
        videoRepository.deleteById(id);
    }

    @Override
    public Video getById(UUID id) {
        VideoEntity db = videoRepository.findById(id).get();
        Video videoDto = new Video();
        BeanUtils.copyProperties(db, videoDto);
        return videoDto;
    }
}
